import numpy as np
from sklearn import metrics
def contingency_table(label, result):
    total_num = len(label)
    TP = TN = FP = FN = 0
    for i in range(total_num):
        for j in range(i + 1, total_num):
            if label[i] == label[j] and result[i] == result[j]:
                TP += 1
            elif label[i] != label[j] and result[i] != result[j]:
                TN += 1
            elif label[i] != label[j] and result[i] == result[j]:
                FP += 1
            elif label[i] == label[j] and result[i] != result[j]:
                FN += 1
    return (TP, TN, FP, FN)

def rand_index(label_file, result_file):
    label = np.loadtxt(label_file).tolist()
    result = np.loadtxt(result_file).tolist()
    TP, TN, FP, FN = contingency_table(result, label)
    ri = 1.0 * (TP + TN) / (TP + FP + FN + TN)
    print(ri)
    return(ri)